package com.huashi.dealer.modular.controller.api.cart;

import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.cart.service.CartService;
import com.huashi.dealer.modular.business.cart.param.CartRequestParam;
import com.huashi.dealer.modular.business.cart.param.CartResponseParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * API购物车控制器
 *
 * @author 谭陈强
 * @since 2025-07-30
 */
@Tag(name = "API购物车控制器")
@RestController("apiCartController")
@RequestMapping("/api/cart")
@Validated
public class CartController {

    @Resource
    private CartService cartService;

    /**
     * 加入购物车
     */
    @PostMapping("/add")
    @Operation(summary = "加入购物车")
    public CommonResult<String> addToCart(@RequestBody CartRequestParam requestParam) {
        try {
            cartService.addToCart(requestParam);
            return CommonResult.ok();
        } catch (Exception e) {
            return CommonResult.error("加入购物车失败：" + e.getMessage());
        }
    }

    /**
     * 购物车列表
     */
    @GetMapping("/lists")
    @Operation(summary = "购物车列表")
    public CommonResult<CartResponseParam> getCartList(@RequestParam(required = false) Integer isGoodsType) {
        try {
            CartResponseParam responseParam = cartService.getCartList(isGoodsType);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取购物车列表失败：" + e.getMessage());
        }
    }

    /**
     * 删除购物车商品
     */
    @GetMapping("/delete")
    @Operation(summary = "删除购物车商品")
    public CommonResult<String> deleteCartItem(@RequestParam String goodsSkuId,
                                               @RequestParam(required = false) Integer isGoodsType) {
        try {
            cartService.deleteCartItem(goodsSkuId, isGoodsType);
            return CommonResult.ok("删除成功");
        } catch (Exception e) {
            return CommonResult.error("删除购物车商品失败：" + e.getMessage());
        }
    }

    /**
     * 减少购物车商品数量
     */
    @GetMapping("/sub")
    @Operation(summary = "减少购物车商品数量")
    public CommonResult<String> reduceCartItem(@RequestParam String goodsSkuId,
                                               @RequestParam String goodsId,
                                               @RequestParam(required = false) Integer goodsNum,
                                               @RequestParam(required = false) Integer isGoodsType) {
        try {
            cartService.reduceCartItem(goodsSkuId, goodsId, goodsNum, isGoodsType);
            return CommonResult.ok("减少成功");
        } catch (Exception e) {
            return CommonResult.error("减少购物车商品数量失败：" + e.getMessage());
        }
    }

    /**
     * 清空购物车
     */
    @PostMapping("/clear")
    @Operation(summary = "清空购物车")
    public CommonResult<String> clearCart(@RequestParam String token,
                                          @RequestParam(required = false) Integer isGoodsType) {
        try {
            cartService.clearCart(token, isGoodsType);
            return CommonResult.ok("清空成功");
        } catch (Exception e) {
            return CommonResult.error("清空购物车失败：" + e.getMessage());
        }
    }

    /**
     * 更新购物车商品数量
     */
    @PostMapping("/update")
    @Operation(summary = "更新购物车商品数量")
    public CommonResult<CartResponseParam> updateCartItem(@RequestBody CartRequestParam requestParam) {
        try {
            CartResponseParam responseParam = cartService.updateCartItem(requestParam);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("更新购物车商品数量失败：" + e.getMessage());
        }
    }

    /**
     * 购物车商品数量
     */
    @GetMapping("/count")
    @Operation(summary = "购物车商品数量")
    public CommonResult<CartResponseParam> getCartCount(@RequestParam String token,
                                                        @RequestParam(required = false) Integer isGoodsType) {
        try {
            CartResponseParam responseParam = cartService.getCartCount(token, isGoodsType);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取购物车商品数量失败：" + e.getMessage());
        }
    }
} 